
const  categoryQueryAll = (url) =>  {
	return new Promise((reject,resolve)=>{
		uni.request({
			url: (url || getApp().globalData.baseUrl) + '/category/allFoods',
			method:'GET',
			timeout:5000,
			header:{
				 'content-type': 'application/x-www-form-urlencoded'
			},
			success:res=>{
				if(res.statusCode == 200 && res.data.code == 20000){
					reject(res.data)
					
				}else{
					resolve(new Error(res.data.msg))
				}
			},fail:res=>{			
				resolve(new Error("获取数据失败"))
			}
		})
	})
}




const  flavorQueryAll = () =>  {
 
	return new Promise((reject,resolve)=>{
		
		uni.request({
			url:getApp().globalData.baseUrl + '/flavor/all',
			method:'GET',
			timeout:5000,
			header:{
				 'content-type': 'application/x-www-form-urlencoded'
			},
			success:res=>{
				if(res.statusCode == 200 && res.data.code == 20000){
					reject(res.data)
					
				}else{
					
					resolve(new Error(res.data.msg))
				}
			},fail:res=>{			
				resolve(new Error("获取数据失败"))
			
			}
			
		})
	})
}

const  placeQueryAll = () =>  {

	return new Promise((reject,resolve)=>{
		
		uni.request({
			url:getApp().globalData.baseUrl + '/place/all',
			method:'GET',
			timeout:5000,
			header:{
				 'content-type': 'application/x-www-form-urlencoded'
			},
			success:res=>{
				if(res.statusCode == 200 && res.data.code == 20000){
					reject(res.data)
					
				}else{
					
					resolve(new Error(res.data.msg))
				}
			},fail:res=>{			
				resolve(new Error("获取数据失败"))
			
			}
			
		})
	})
}

const  orderCommit = (order) =>  {
	return new Promise((reject,resolve)=>{
		uni.request({
			url:getApp().globalData.baseUrl + '/order/save',
			method:'POST',
			timeout:5000,
			data:{
				order
			},
			success:res=>{
				console.log(res)
				if(res.statusCode == 200 && res.data.code == 20000){
					reject(res.data)
				}else{
					resolve(new Error(res.data.msg))
				}
			},fail:res=>{			
				resolve(new Error("获取数据失败"))
			}
		})
	})
}
const queryOrderById = id =>{
	return new Promise((reject,resolve)=>{
		uni.request({
			url:getApp().globalData.baseUrl + `/order/${id}`,
			method:'GET',
			timeout:5000,
			success:res=>{
				if(res.statusCode == 200 && res.data.code == 20000){
					reject(res.data)
				}else{
					resolve(new Error(res.data.msg))
				}
			},fail:res=>{			
				resolve(new Error("获取数据失败"))
			}
		})
	})
}

const queryOrderAll = (pageNum,limit) =>{
	return new Promise((reject,resolve)=>{
		uni.request({
			url:getApp().globalData.baseUrl + `/order/all`,
			method:'POST',
			timeout:5000,
			header:{
				 'content-type': 'application/x-www-form-urlencoded'
			},data:{
				page:pageNum,
				limit:limit
			},
			success:res=>{
				if(res.statusCode == 200 && res.data.code == 20000){
					reject(res.data)
				}else{
					resolve(new Error(res.data.msg))
				}
			},fail:res=>{			
				resolve(new Error("获取数据失败"))
			}
		})
	})
}

const notifyMeal = id =>{
	return new Promise((reject,resolve)=>{
		uni.request({
			url:getApp().globalData.baseUrl + `/order/notify/${id}`,
			method:'GET',
			timeout:5000,
			success:res=>{
				if(res.statusCode == 200 && res.data.code == 20000){
					reject(res.data)
				}else{
					resolve(new Error(res.data.msg))
				}
			},fail:res=>{			
				resolve(new Error("获取数据失败"))
			}
		})
	})
}

const orderFinish = id => {
	
	return new Promise((reject,resolve)=>{
		uni.request({
			url:getApp().globalData.baseUrl + `/order/finish/${id}`,
			method:'GET',
			timeout:5000,
			success:res=>{
				if(res.statusCode == 200 && res.data.code == 20000){
					reject(res.data)
				}else{
					resolve(new Error(res.data.msg))
				}
			},fail:res=>{			
				resolve(new Error("获取数据失败"))
			}
		})
	})
	
}

const getBannerList = (url) => {
	return new Promise((reject,resolve)=>{
		uni.request({
			url: (url || getApp().globalData.baseUrl ) + `/banner/all`,
			method:'GET',
			timeout:5000,
			success:res=>{
				if(res.statusCode == 200 && res.data.code == 20000){
					reject(res.data)
				}else{
					resolve(new Error(res.data.msg))
				}
			},fail:res=>{			
				resolve(new Error(res.msg))
			}
		})
	})
	
}



const getAddMealOrder = () => {
	
	return new Promise((reject,resolve)=>{
		uni.request({
			url:getApp().globalData.baseUrl + `/order/getAddMealOrder`,
			method:'GET',
			timeout:5000,
			success:res=>{
				if(res.statusCode == 200 && res.data.code == 20000){
					reject(res.data)
				}else{
					resolve(new Error(res.data.msg))
				}
			},fail:res=>{			
				resolve(new Error(res.msg))
			}
		})
	})
	
}


module.exports = {
	categoryQueryAll,
	flavorQueryAll,
	placeQueryAll,
	orderCommit,
	queryOrderById,
	queryOrderAll,
	notifyMeal,
	orderFinish,
	getAddMealOrder,
	getBannerList
}